/**
 * getAreaList   获取报修区域列表
 * queryAreaList 根据name模糊查询报修区域
 * addArea       添加报修区域
 * getInfoArea   获取报修区域信息
 * updateArea    修改报修区域信息
 * deleteArea    删除报修区域
 */
const areaStatement = require("../../common/sql_sentences/area")
const repairerStatement = require("../../common/sql_sentences/repairer")
const repairerTypeStatement = require("../../common/sql_sentences/repairer_type")

exports.getAreaList = async (req, res) => {
  try {
    let { page, pagesize } = req.query
    // 获取报修区域总数
    let total,
      sqlStr = areaStatement.selectCount()
    let results = await RunSQL(sqlStr)
    total = results[0].total

    if (!pagesize) pagesize = total

    // 获取报修区域列表
    sqlStr = areaStatement.selectLimit(page, pagesize)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.screenAreaList = async (req, res) => {
  try {
    // 获取报修区域列表
    sqlStr = `select hex(id) id, name from area`
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, results)
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryAreaList = async (req, res) => {
  try {
    const data = req.query
    // 根据name模糊查询报修区域总数
    let total,
      sqlStr = areaStatement.selectCountLike(data.name)
    let results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("没有找到相关类型！", 10005)
    total = results[0].total

    // 根据name模糊查询获取报修区域列表
    sqlStr = areaStatement.selectLikeLimit(data.name, data.page, data.pagesize)
    console.log(sqlStr)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.addArea = async (req, res) => {
  try {
    const data = req.body
    // 查询报修区域名称是否重复
    let sqlStr = areaStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("名称已存在！", 10005)

    // 添加报修区域
    sqlStr = areaStatement.insert(req.body)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("添加报修区域失败！", 10005)
    return res.successSend("添加报修区域成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoArea = async (req, res) => {
  try {
    // 获取报修区域信息
    let sqlStr = areaStatement.idSelect(req.body.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("获取报修区域信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateArea = async (req, res) => {
  try {
    const data = req.body
    // 查询报修区域名称是否重复
    let sqlStr = areaStatement.select({ name: data.name })
    let results = await RunSQL
    if (results[0]?.id && results[0]?.id !== data.id)
      return res.errSend("名称已存在！", 10005)

    // 修改报修区域信息
    sqlStr = areaStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改报修区域信息失败！", 10005)
    return res.successSend("修改报修区域信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteArea = async (req, res) => {
  try {
    const id = req.params.id
    // 获取报修类型
    let sqlStr = repairerTypeStatement.select({ a_id: id })
    let results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("删除报修区域失败!有报修类型属于该报修区域！", 10005)

    // 获取维修人员信息
    sqlStr = repairerStatement.select({ a_id: id })
    results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("删除报修区域失败!有维修人员属于该报修区域！", 10005)

    // 删除报修区域
    sqlStr = areaStatement.delete(id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("删除报修区域失败！", 10005)
    return res.successSend("删除报修区域信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}
